home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16204 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.7 KB

  1. Path: mn4.swip.net!funsys!funsys.se
  2. From: rrg@funsys.se (Robin Rosenberg)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Would/Won't you use a garbage collector?
  5. Message-ID: <316B6CA4.4F30@funsys.se>
  6. Date: 10 Apr 96 08:09:08 GMT
  7. References: <4kamie$e4d@dfw-ixnews3.ix.netcom.com>
  8. Sender: usenet@funsys.funsys.se
  9. Organization: Enator Objective Management AB
  10. X-Mailer: Mozilla 2.0 (Win95; I; 16bit)
  11. MIME-Version: 1.0
  12. Content-Type: text/plain; charset=us-ascii
  13. Content-Transfer-Encoding: 7bit
  14.  
  15. Giuliano Carlini wrote:
  16. > I'm a long time proponent of using garbage collection in C and C++
  17. > programs, and I'm curious:
  18. >         - How many others are there?Count One. It was no problem to use it and it was fairly fast. It was
  19. a small compiler. Version one was a command-line program that only 
  20. allocated memory until it was finished and then the whole program exited.
  21. The second version was a Windoze version and suddenly memory had to be
  22. freed between invocations. Using GC was very handy and appropriate.
  23.  
  24. >         - Why don't most C/C++ programmers use it?If you are not 100% sure it will work, you will put your project at
  25. risk. Secondly your program will use more memory with GC than it
  26. would otherwise (unless your memory leaks are severe). Alternatively 
  27. you could perform GC very often at the cost of CPU and response time.
  28.  
  29. > I'm particularly interested in finding out why most C/C++ don't use it.
  30. > While I have my own theories - which I'll describe below - I'm
  31. > interested
  32. > in finding out more directly from those who are against it. I've spent
  33. > a
  34. > good part of the past 4 years trying to convince people to use it, and
  35. > I've got a handle on why the small group of people I associate with are
  36. > not using it, but I'd like to find out more about why the C/C++
  37. > community
  38. > as a whole does not.
  39.  
  40. Memory is but one resource. The other resources are trickier 
  41. as they often have to be freed in a particular order and conservative 
  42. GC's have their own ideas of when to free resources and their order.
  43. Secondly if you use global things like notifications (as in MVC) you
  44. will end up managing your resources explicitly anyway. Weak pointers
  45. may help there but you have to design for that and the reason for using 
  46. GC was that you didn't want to bother in the first place.
  47.  
  48. Secondly GC assumes you can take over memory management in the whole
  49. application. How about shared libraries (DLL's etc). You don't want
  50. to static linke (even if you could) your whole application.
  51.  
  52. > I hope that with this information, I'll be able to perfect a more
  53. > convincing presentation for why the default should be to use garbage
  54. > collection, and for why explicit calls to free/delete should be used
  55. > only
  56. > in the rare cases for which garbage collection is inappropriate.
  57. > What follows is my belief for why garbage collection is so little used.
  58. > Feel free to respond to anything I say below, but please, first respond
  59. > to the questions above. I believe that most people don't use garbage
  60. > collection because either they:
  61. >         - don't know what it is
  62. >         - don't know it can be used with C/C++.
  63. >         - are misinformation
  64. >         - are biased against it by the C/C++ culture
  65.  
  66. Probably true on all points. Regarding the last point, one might add that
  67. GC is not a replacement for proper design, which also solves the memory
  68. managemnt problem, sometimes at a higher design expense.
  69.  
  70. > In my experience, most C/C++ programmers either don't know what garbage
  71. > collection is, or don't know that it can be used with C/C++. After all,
  72. > no major C/C++ compiler includes a garbage collector. At least, as far
  73. > as I know. I hope I'm wrong, and that someone can correct me. But even
  74. > after, I tell them what it is, and that it can be used with C++, almost
  75. > everyone still rejects it.
  76.  
  77. One think is that most GC's are free or shareware with little or no support
  78. and very few programmers are capable of troubleshooting problems related to
  79. GC. A light can be seen at the other end of the tunnel, though. I saw an ad
  80. in Object Magazine for a company that claims to have a plug-and-play GC for
  81. sale.
  82.  
  83. > At first, most offer technical reasons for rejecting it. Almost all are
  84. > based on misinformation, since garbage collection is usable and
  85. > benificial
  86. > for the vast majority of systems. The most often mentioned is that
  87. >[.strange jump in article.]
  88. [...]
  89. >inventory control. And what else is memory deallocation but a sort of
  90. >bookkeeping or inventory control. It's just that rather than being for
  91. >a resource outside of the computer it is for one inside.
  92.  
  93. You can also use the idea of ownership (has-relations or whatever
  94. you call it). Make sure you always have an owner to your objects and
  95. make sure the owner deletes everything it owns. That goes a long
  96. way towards ridding yourself of memory leaks. auto_ptr and similar
  97. idioms are also a must.
  98.  
  99. >For anyone who may be wondering, I believe that we should use garbage
  100. >collection because:
  101. >        - It vastly decreases the number of bugs in programs which use
  102. >it.
  103.  
  104. but it can introduce very subtle bugs. Since GC can happen at "any"
  105. time it is hard to find out what is the problem when problems occur.
  106.  
  107. >        - It vastly decreases the time to complete programs which use
  108. >it.
  109.  
  110. More or less true.
  111.  
  112. >        - It vastly increases the understandability, reuseability,
  113. >                extensability, and maintainability of programs which use
  114. >it.
  115.  
  116. Indeed. A lot of code can be removed. One should always consider GC. 
  117.  
  118. >So, do you use garbage collection in your C/C++ programs, and if not,
  119. >why not?
  120.  
  121. >giuliano-- 
  122. Robin Rosenberg,  | Voice: +46-8-7036200 | OO consulting, education
  123. Enator Objective  | Fax:   +46-8-7036283 | and mentoring since
  124.  Management AB    | Mail:  rrg@funsys.se | 1985.
  125. "Any opinions above are my own *personal* opinions, etc. etc."
  126.